/**
 * 
 */
package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.JLayeredPane;

import common.DataManager;
import common.FontFactory;

/**
 *A layered pane which has a white background, an image, and possibly a 
 *highlighted frame around the image for when the item is selected.
 *This is extended by both cargoItemPanel and StoreItemPanel, which just change
 *the way it responds to mouse clicks
 */
public class ItemIconPanel extends JLayeredPane {
	
	private static final int ICON_WIDTH = 50;
	private static final int ICON_HEIGHT = 50;
	
	private String itemName;
	private boolean isCovered = false;
	
	ImagePanel whiteBg = new ImagePanel(DataManager.INVENTORY_ICON_BACKGROUND);
	ImagePanel highlight = new ImagePanel(DataManager.INVENTORY_ICON_SELECTED);
	ImagePanel unavailableCover = new ImagePanel(DataManager.UNAVAILABLE_ITEM_ICON_COVER);
	ImagePanel iconCover = new ImagePanel(DataManager.ICON_COVER);
	JLabel bottomLabel = new JLabel("XXX");
	JLabel topLabel = new JLabel("YYY");
	ImagePanel item;
	/**
	 * Constructs a blank white square
	 */
	public ItemIconPanel(){
		super();
		this.add(whiteBg,100);
		this.moveToBack(whiteBg);
		this.setPreferredSize(new Dimension(ICON_WIDTH, ICON_HEIGHT));
		this.setSize(new Dimension(ICON_WIDTH, ICON_HEIGHT));
	}
	
	/**
	 * Constructs a white square with the specified image over top of it
	 * 
	 * @param fileName
	 */
	public ItemIconPanel(String fileName){
		this(fileName, "", "");
	}
	
	/**
	 * Constructs a white square with the specified image over top of it
	 * 
	 * @param fileName, bottomLabel
	 */
	public ItemIconPanel(String fileName, String bottomLabel){
		this(fileName, "", bottomLabel);
	}
	
	/**
	 * Constructs a white square with the specified image over top of it
	 * 
	 * @param fileName bottomLabel, topLabel
	 */
	public ItemIconPanel(String fileName, String topLabel, String bottomLabel){
		this();
		this.itemName = fileName;
		item = new ImagePanel(fileName);
		
		this.add(this.topLabel,0);
		this.add(this.bottomLabel, 1);
		this.add(item,5);
		this.add(highlight,4);
		this.add(unavailableCover,3);
		this.add(iconCover,2);
		
		this.moveToFront(item);
		this.moveToFront(iconCover);
		this.moveToFront(this.topLabel);
		this.moveToFront(this.bottomLabel);
		this.topLabel.setBounds(0,0,ICON_WIDTH,ICON_HEIGHT/3);
		this.bottomLabel.setBounds(0,ICON_HEIGHT-ICON_HEIGHT/3,ICON_WIDTH, ICON_HEIGHT/3);
		item.setBounds(0, 0, ICON_WIDTH, ICON_HEIGHT);
		highlight.setBounds(0, 0, ICON_WIDTH, ICON_HEIGHT);
		unavailableCover.setBounds(0, 0, ICON_WIDTH, ICON_HEIGHT);
		iconCover.setBounds(0, 0, ICON_WIDTH, ICON_HEIGHT);
		
		setBottomLabel(bottomLabel);
		setTopLabel(topLabel);
		this.topLabel.setForeground(Color.BLUE);
		this.bottomLabel.setForeground(Color.WHITE);
		this.unavailableCover.setOpaque(false);

		highlight.setVisible(false);
		unavailableCover.setVisible(false);
	}
	
	/**
	 * adds a highlight to the item icon
	 * 
	 */
	public void highlightItem(){
		moveToFront(highlight);
		moveToFront(iconCover);
		moveToFront(bottomLabel);
		moveToFront(topLabel);
		highlight.setVisible(true);
	}
	
	/**
	 * removes a highlight to the item icon
	 * 
	 */
	public void unhighlightItem(){
		moveToBack(highlight);
		highlight.setVisible(false);
	}
	
	/**
	 * adds a unavailable item cover to the item icon
	 * 
	 */
	public void coverItem(){
		isCovered = true;
		moveToFront(unavailableCover);
		moveToFront(iconCover);
		moveToFront(bottomLabel);
		moveToFront(topLabel);
		unavailableCover.setVisible(true);
	}
	
	/**
	 * removes the unavailable item cover from the icon
	 * 
	 */
	public void uncoverItem(){
		isCovered = false;
		moveToBack(unavailableCover);
		unavailableCover.setVisible(false);
	}
	
	//getters and setters
	public static int getIconWidth(){
		return ICON_WIDTH;
	}
	public static int getIconHeight(){
		return ICON_HEIGHT;
	}
	public String getItemName(){
		return itemName;
	}
	
	public void setBottomLabel(String label){
		this.bottomLabel.setText(label);
		FontFactory fontFactory = new FontFactory();
		Font labelFont = this.bottomLabel.getFont();

		int stringWidth = this.bottomLabel.getFontMetrics(labelFont).stringWidth(label);
		int componentWidth = this.bottomLabel.getWidth();

		double widthRatio = (double)componentWidth / (double)stringWidth;

		int newFontSize = (int)(labelFont.getSize() * widthRatio);
		int componentHeight = this.bottomLabel.getHeight();
		
		int fontSizeToUse = Math.min(newFontSize, componentHeight);
		
		this.bottomLabel.setFont(fontFactory.createFont(fontSizeToUse));
	}
	public void setTopLabel(String label){
		this.topLabel.setText(label);
		FontFactory fontFactory = new FontFactory();
		Font labelFont = this.topLabel.getFont();

		int stringWidth = this.topLabel.getFontMetrics(labelFont).stringWidth(label);
		int componentWidth = this.topLabel.getWidth();

		double widthRatio = (double)componentWidth / (double)stringWidth;

		int newFontSize = (int)(labelFont.getSize() * widthRatio);
		int componentHeight = this.topLabel.getHeight();

		int fontSizeToUse = Math.min(newFontSize, componentHeight);
		
		this.topLabel.setFont(fontFactory.createFont(fontSizeToUse));
	}
	public boolean isCovered(){
		return isCovered;
	}
	
	
}
